package it.fuesi.expired;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import it.fuesi.expired.fragments.ExpiredFragment;
import it.fuesi.expired.fragments.NotExpiredFragment;
import it.fuesi.expired.fragments.TagsFragment;

public class MainActivity extends Activity {

	private static final String TAG = "ExpiredMain";

    public static final int FRG_POSITION_EXPIRED = 0;
    public static final int FRG_POSITION_NOT_EXPIRED = 1;
    public static final int FRG_POSITION_TAGS = 2;

    private int currentFragmentId = FRG_POSITION_EXPIRED;

	private boolean portait = true;
	
	private String[] menuArray;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    
    private ActionBarDrawerToggle mDrawerToggle;
    
    private static final int CURRENT_API = Integer.valueOf(android.os.Build.VERSION.SDK);
    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        portait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
        Log.d(TAG, "isOrientationPortrait? " + portait);
        
        menuArray = getResources().getStringArray(R.array.main_menu);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        // Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, menuArray));
        // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
        mDrawerTitle = getString(R.string.menu);
        // enable ActionBar app icon to behave as action to toggle nav drawer
        
        getActionBar().setHomeButtonEnabled(true);
        
        Log.d("LOG", "Current api level: " + CURRENT_API);
        
        if(CURRENT_API >= 18){
        	getActionBar().setDisplayHomeAsUpEnabled(true);
        	getActionBar().setHomeAsUpIndicator(R.drawable.ic_drawer);
        }
        
        if(portait)
        	initPortrait();
        
        if (savedInstanceState == null) {
            selectItem(0);
        }

    }
    
    private void initPortrait(){
    	mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    	mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
            
        };
        
        mDrawerLayout.setDrawerListener(mDrawerToggle);

    }
    
    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	int itemId = item.getItemId();
        
    	Log.d(TAG, "onMenuItemSelected --> " + itemId);
    	
    	switch (itemId) {
            case android.R.id.home:
                if(mDrawerLayout.isDrawerOpen(mDrawerList))
                    mDrawerLayout.closeDrawer(mDrawerList);
                else
                    mDrawerLayout.openDrawer(mDrawerList);

                // Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
                break;

            case R.id.action_add:
                final Intent intent = FormActivity.makeIntent(this, this.currentFragmentId);
                startActivityForResult(intent, FormActivity.REQUEST_CODE);

                break;

            }

    	return super.onMenuItemSelected(featureId, item);
    }





	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.main, menu);
	    return super.onCreateOptionsMenu(menu);
	}
	
	private class DrawerItemClickListener implements ListView.OnItemClickListener {
	    @Override
	    public void onItemClick(AdapterView parent, View view, int position, long id) {
	        selectItem(position);
	    }
	}


	/** Swaps fragments in the main content view */
	private void selectItem(int position) {

        this.currentFragmentId = position;

		Fragment fragment;
		
		if(position == FRG_POSITION_EXPIRED)
			fragment = new ExpiredFragment();
		else if(position == FRG_POSITION_NOT_EXPIRED)
			fragment = new NotExpiredFragment();
		else if(position == FRG_POSITION_TAGS)
			fragment = new TagsFragment();
        else
            fragment = null;

        if(fragment == null) return;

	    // Insert the fragment by replacing any existing fragment
	    FragmentManager fragmentManager = getFragmentManager();
	    fragmentManager.beginTransaction()
	                   .replace(R.id.content_frame, fragment)
	                   .commit();

	    // Highlight the selected item, update the title, and close the drawer
	    mDrawerList.setItemChecked(position, true);
	    setTitle(menuArray[position]);
	    
	    if(portait)
	    	mDrawerLayout.closeDrawer(mDrawerList);
	}

	@Override
	public void setTitle(CharSequence title) {
	    mTitle = title;
	    getActionBar().setTitle(mTitle);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(event.getAction() == MotionEvent.EDGE_RIGHT)
			Log.d(TAG, "Edge right");
		return super.onTouchEvent(event);
	}


}


